/**
 * Created by Administrator on 2017/8/24 0024.
 */
var common = require('../utils/common'),
    securityModel = require('../models/security'),
    logInfoBiz = require('./log-info-biz'),
    _ = require('underscore'),
    security = {
    saveLoginFail: function (param,callback) {
        var record = {},
            strTime = common.dateFormatter(new Date(),"yyyy-MM-dd hh:mm:ss"),
            queryParam = {
                ip: param.ip,
                funcid: param.funcid,
                date: common.dateFormatter(new Date(),'yyyy-MM-dd')
            };
        this.findRecord(param,function(doc){
            if(doc.length > 0){
                _.extend(record,doc[0]._doc);
                record.count++;
                securityModel.update(queryParam,{$set :{count: record.count}},callback);
            }else{
                record = {
                    ip: param.ip,
                    funcid: param.funcid,
                    date: common.dateFormatter(new Date(),'yyyy-MM-dd'),
                    count: 0,
                    input: param.input,
                    remark: "登陆异常"
                };
                securityModel.create(record,callback);
            }
            logInfoBiz.create({
                ip: param.ip,
                url: param.funcid,
                type: 'POST',
                remark: strTime+"【 登陆异常："+ param.ip+"】,input="+param.input
            })
        });
    },
    findRecord: function(param,callback){
        var queryParam = {
            ip: param.ip,
            funcid: param.funcid,
            date: common.dateFormatter(new Date(),'yyyy-MM-dd')
        };
        securityModel.find(queryParam).sort({input:"asc"}).exec(function(err,doc){
            callback(doc)
        });
    }
};
module.exports = security;